import java.sql.*;

public class MySQLConn {
    private static final String url = "jdbc:mysql://192.168.1.41:3306/jdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
    private static final String username = "db_user";
    private static final String password = "MySQL123456@";

    private static Connection connection = null;
    private static CallableStatement stmt = null;

    public void connect() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException | SQLException e) {
            e.fillInStackTrace();
        }
    }

    public void disconnect() {
        try {
            if (stmt != null) stmt.close();
            if (connection != null) connection.close();
        } catch (SQLException e) {
            e.fillInStackTrace();
            //
        }
    }

    public synchronized void writeLog(String log) {
        try {
            if (connection != null) {
                String sql = "call insert_log(?);"; //有几个参数就几个问号
                stmt = connection.prepareCall(sql);
                stmt.setString(1, log);
                stmt.execute();
            }
        } catch (SQLException e) {
            e.fillInStackTrace();
        }
    }
}